#include <FL/fl_ask.H>
#include <cmath>

#include "ScissorDoc.h"
#include "ScissorUI.h"

ScissorDoc::ScissorDoc() {
	m_imageName[0] = '\0';
	m_nWidth = -1;
	m_ucBitmap = NULL;
}

void ScissorDoc::setUI(ScissorUI* ui) {
	m_pUI = ui;
}

ScissorUI* ScissorDoc::getUI() {
	return m_pUI;
}

int ScissorDoc::loadImage(char* iname) {
	// try to open the image to read
	unsigned char*	data;
	int				width, 
					height;

	if ( (data=readBMP(iname, width, height))==NULL ) 
	{
		fl_alert("Can't load bitmap file");
		return 0;
	}

	fl_message("hi");
	// reflect the fact of loading the new image
	m_nWidth		= width;
//	m_nPaintWidth	= width;
	m_nHeight		= height;
//	m_nPaintHeight	= height;

	// release old storage
	if ( m_ucBitmap ) delete [] m_ucBitmap;
//	if ( m_ucPainting ) delete [] m_ucPainting;
//	if ( cost_graph ) delete [] cost_graph;

	m_ucBitmap		= data;

	// allocate space for draw view
//	m_ucPainting	= new unsigned char [width*height*3];
//	memset(m_ucPainting, 0, width*height*3);

	m_pUI->m_mainWindow->resize(m_pUI->m_mainWindow->x(), 
								m_pUI->m_mainWindow->y(), 
								width*2, 
								height+25);
	// calculate the cost graph
/*	cost_graph = new PixelNode [width*height];
	cal_cost_graph();
*/
	// display it on origView
	fl_message("2");
	m_pUI->m_origView->resizeWindow(width, height);	
	fl_message("3");
	m_pUI->m_origView->refresh();
	fl_message("4");

	// refresh paint view as well
/*	m_pUI->m_paintView->resizeWindow(width, height);	
	m_pUI->m_paintView->refresh();
*/	return 1;
}

int ScissorDoc::saveImage(char* iname) {
	return 1;
}

char* ScissorDoc::getImageName() {
	return m_imageName;
}
